package com.xm.clj.underserver.utils;

import com.xm.clj.underserver.bean.RegUser;
import com.xm.clj.underserver.bean.Room;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Component
public class DBUtils {

    private static DBUtils dbUtils;

    @PostConstruct
    public void init(){
        dbUtils = this;
    }

    @Autowired
    JdbcTemplate jdbcTemplate;



    public static boolean findUser(String id){

        String sql = "select * from user where account_id = ?";
        Object[] o = new Object[1];
        o[0] = id;
        List<Map<String, Object>> ans = dbUtils.jdbcTemplate.queryForList(sql, o);
        return ans.size()==0;
    }

    public static String findUsername(String id){
        System.out.println("id : "+id);
        String sql = "select * from user where account_id = ?";
        Object[] objects = new Object[1];
        objects[0] = id;
        Map<String,Object> map = dbUtils.jdbcTemplate.queryForMap(sql,objects);
        return (String) map.get("username");
    }

    public static List<Map<String,Object>> findGameUser(String room){
        String sql = "select * from game where roomId = ?";
        Object[] objects = new Object[1];
        objects[0] = room;
        return dbUtils.jdbcTemplate.queryForList(sql,objects);
    }

    public static int writeIntoUser(RegUser regUser){
        String sql = "insert into user(account_id,username,password) values(?,?,?)";
        Object[] o = new Object[3];
        o[0] = regUser.getAccount_id();
        o[1] = regUser.getUsername();
        o[2] = regUser.getPassword();
        int ans = dbUtils.jdbcTemplate.update(sql,o);
        return ans;
    }

    public static boolean findPassword(String id,String password){
        String sql = "select * from user where account_id = ?";
        Object[] o = new Object[1];
        o[0] = id;
        List<Map<String, Object>> ans = dbUtils.jdbcTemplate.queryForList(sql, o);
        for (Map<String, Object> map : ans){
            if (!map.get("password").equals(password)){
                return false;
            }
        }
        return true;
    }

    public static String createRoom(Room room){
        // 生成随机房间号
        String room_id = selectNum();
        room.setRoom_id(room_id);
        // 记录到数据库
        int ans = insertRoom(room);
        if (ans>0){
            System.out.println("ans>0");
            System.out.println(room_id);
            return room_id;
        }
        return "";
    }

    public static boolean findRoom(String roomId){
        String sql = "select * from room where room_id ="+roomId;
        System.out.println(sql);
        List<Map<String,Object>> list = dbUtils.jdbcTemplate.queryForList(sql);
        System.out.println(list.size());
        return !list.isEmpty();
    }

    private static int insertRoom(Room room){
        String sql = "insert into room(room_id) values(?)";
        Object[] o = new Object[1];
        o[0] = room.getRoom_id();
        int ans = dbUtils.jdbcTemplate.update(sql,o);
        System.out.println("ans "+ans);
        return ans;
    }

    private static boolean isSameNum(String num){
        String sql = "select * from room where room_id = ?";
        Object[] o = new Object[1];
        o[0] = num;
        return dbUtils.jdbcTemplate.queryForList(sql,o).size()>0;
    }

    private static String selectNum() {
        Random random = new Random();
        boolean flag = true;
        while(flag) {
            String base = "";
            for (int i = 0; i < 4; i++) {
                if (random.nextInt(10) > 5) {
                    base += (char)('a' + random.nextInt(26));
                } else {
                    base += random.nextInt(10);
                }
            }
            System.out.println("房间号： " + base);
            // 查询数据库，是否有相同的房间号
            if (!isSameNum(base)) {
                flag = false;
                System.out.println("最终的房间号： " + base);
                return base;
            }
        }
        return "";
    }
    public static String db_index2Id(String index,String room){
        String sql = "select * from game where userindex = ? and roomId = ?";
        Object[] objects = new Object[2];
        objects[0] = index;
        objects[1] = room;
        Map<String,Object> map = dbUtils.jdbcTemplate.queryForMap(sql,objects);
        if (map!=null){
            return (String) map.get("userId");
        }
        return "";
    }

    public static List<Map<String,Object>> findUsers(String roomId){
        String sql = "select * from game where roomId = ?";
        Object[] objects = new Object[1];
        objects[0] = roomId;
        return dbUtils.jdbcTemplate.queryForList(sql,objects);
    }

    public static String[] findUserInRoom(String roomId){
        String sql = "select * from room where roomId = ?";
        Object[] objects = new Object[1];
        objects[0] =  roomId;
        List<Map<String,Object>> list = dbUtils.jdbcTemplate.queryForList(sql,objects);
        Map<String,Object> map = list.get(0);
        String curUsers = (String)map.get("users");
        String[] s = curUsers.split(";");
        return s;
    }

    public static void db_outRoom(String user,String room){
        String sql = "delete from game where userId = ? and roomId = ?";
        Object[] objects = new Object[2];
        objects[0] = user;
        objects[1] = room;
        dbUtils.jdbcTemplate.update(sql,objects);
    }

    public static void db_enterRoom(String user,String room,String index){
        // 查找user id是否存在
        String str = "delete from game where userId = ?";
        Object[] o = new Object[1];
        o[0] = user;
        dbUtils.jdbcTemplate.update(str,o);


        String sql = "insert into game(userId,roomId,userindex) values(?,?,?)";
//        String sql = "insert into game(userId,room,index) values(?,?,?)";
        Object[] objects = new Object[3];
        objects[0] = user;
        objects[1] = room;
        objects[2] = index;
        dbUtils.jdbcTemplate.update(sql, objects);

    }

    public static Map<String,Object> db_findUserInfo(String user){
        String sql = "select * from game where userId = ?";
        Object[] objects = new Object[1];
        objects[0] = user;
        return dbUtils.jdbcTemplate.queryForList(sql,objects).get(0);
    }
}
